﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using SIPO.Library.DomainObject.Administration.UserManagement;
using SIPO.Library.DataAccess.Administration.UserManagement;
using System.Configuration;

using SIPO.Web.Support;

namespace SIPO.Web.Forms.Administration.UserManagement
{
    public partial class sMenuform : BasePage
    {
        private int id = -1;

        protected override void OnLoad(EventArgs e)
        {
            if (Request.Params["Id"] != null)
            {
                id = Convert.ToInt32(Request.Params["Id"]);
            }

            if (!Page.IsPostBack)
            {
                recMenuRepo menuDal = new recMenuRepo(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);
                List<recMenu> menus = menuDal.GetHierarchyList("");
                ddlParent.Items.Clear();
                menus.ForEach(delegate(recMenu menu)
                {
                    ddlParent.Items.Add(new ListItem(menu.MenuName, menu.MenuID.ToString()));
                });

                ddlParent.Items.Insert(0, new ListItem("-- pilih menu parent --", string.Empty));

                if (id > 0) // Edit an existing item
                {
                    recMenu menu = menuDal.GetById(id);
                    if (menu != null)
                    {
                        hfID.Value = id.ToString();
                        txtName.Text = menu.MenuName;
                        txtUrl.Text = menu.URL;

                        txtOrderSeq.Text = (menu.OrderSeq.HasValue) ? menu.OrderSeq.Value.ToString() : string.Empty;

                        if (menu.ParentMenuID.HasValue)
                        {
                            ddlParent.ClearSelection();
                            ddlParent.Items.FindByValue(menu.ParentMenuID.Value.ToString()).Selected = true;
                        }
                    }
                }

                GetPrivilege();

            }

        }

        private void GetPrivilege()
        {
            Membership membership = new Membership(ConfigurationManager.ConnectionStrings["SIPOConn"].ConnectionString);

            List<recAccessRights.AccessRights> accessRights = membership.GetAccessRights(base.UserID, base.MenuID);

            accessRights.ForEach(delegate(recAccessRights.AccessRights access)
            {
                switch (access)
                {
                    case (recAccessRights.AccessRights.View):
                        ClientScript.RegisterStartupScript(typeof(Page), "view", "$('#btnSave').button('disable');", true);
                        break;
                    case (recAccessRights.AccessRights.Add):
                        ClientScript.RegisterStartupScript(typeof(Page), "add", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.Edit):
                        ClientScript.RegisterStartupScript(typeof(Page), "edit", "$('#btnSave').button('enable');", true);
                        break;
                    case (recAccessRights.AccessRights.NoAccess):
                        var url = Helper.GetAppVirtualPath(Page);
                        ClientScript.RegisterStartupScript(typeof(Page), "noAccess", "alert('Anda tidak mempunyai hak akses ke halaman ini!'); window.location = '" + url + "'", true);
                        break;
                }
            });

        }

    }
}
